package com.conf.web;

import com.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.apache.http.client.utils.URIBuilder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件删除
 * 获取工程路径 :  https://www.iteblog.com/java_get_file_absolute_path/
 */
@Component
@Slf4j
public class FileDel {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${file.del}")
    private String url;
    /**
     * File路径以 &&拼接的
     * @param file
     */
    public void del(String file){
       if(StringUtils.isEmpty(file)){
           return;
       }
       try {
           MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
           map.add("file",file);
//           URIBuilder builder = new URIBuilder(url);
//           for(Map.Entry<String,String> entry:map.entrySet()){
//                builder.addParameter(entry.getKey(),entry.getValue());
//           }
           Boolean body = restTemplate.postForEntity(url, map,Boolean.class).getBody();
           if(body){
               log.info("删除文件：{}",file);
           }
           else{
               log.info("未删除文件：{}",file);
           }
       }catch (Exception e){
                e.printStackTrace();
       }
    }
    public void path() throws Exception{
        File f = new File(this.getClass().getResource("/").getPath());
        System.out.println(f);

        //当前工程的相对路径
        File directory = new File("");//参数为空
        String courseFile = directory.getCanonicalPath() ;
        System.out.println(courseFile);
        System.out.println(directory.getAbsolutePath());
        System.out.println(this.getClass().getClassLoader().getResource("") );
        System.out.println(System.getProperty("user.dir"));
        System.out.println( System.getProperty("java.class.path"));
        String os = System.getProperty("os.name");
        System.out.println(os);
        if(os.toLowerCase().startsWith("win")){
            System.out.println(os + " can't gunzip");
        }else{
        }
    }


    public static void main(String[] args) throws Exception{
        FileDel fileDel = new FileDel();
        fileDel.path();
    }
}
